@inherits VocaDb.Web.Code.HelperPage
@using System.Globalization
@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using VocaDb.Model.DataContracts.Songs;
@using VocaDb.Model.Domain.Images
@using VocaDb.Model.Domain.Songs
@using VocaDb.Model.Helpers
@using VocaDb.Model.Service.Search.AlbumSearch
@using VocaDb.Model.Utils
@using VocaDb.Web.Helpers;

@helper ArtistParticipationStatusOptionsKnockout(string binding) {
	{
		var options = new Dictionary<string, string> {
			{ ArtistAlbumParticipationStatus.Everything.ToString(), "Everything" },
			{ ArtistAlbumParticipationStatus.OnlyMainAlbums.ToString(), "Only main songs" },
			{ ArtistAlbumParticipationStatus.OnlyCollaborations.ToString(), "Only collaborations" },
		};
		@KnockoutHelpers.CheckBoxButtons(options, binding)
	}
}

@helper LyricsInfo(LyricsForSongContract lyrics) {
	@:@lyrics.TranslationType
	if (!string.IsNullOrEmpty(lyrics.CultureCode)) {
		@:(@CultureInfo.GetCultureInfo(lyrics.CultureCode).EnglishName)
	}
	if (!string.IsNullOrEmpty(lyrics.Source)) {
		@:(@lyrics.Source)
	}
}

@helper PrintArchivedSongData(ComparedSongsContract comparedSongs) {

	<div class="well well-transparent archived-entry-contents">
		<h4>Content</h4>

		<table class="table table-bordered">
			@ArchivedEntryHelpers.DataRow("Id", comparedSongs.FirstData.Id)
			@ArchivedEntryHelpers.TranslatedNameRow(comparedSongs, d => d.TranslatedName)
			@ArchivedEntryHelpers.DataRowList("Names", comparedSongs, d => DataFormatUtils.GenerateHtmlFromHelper(d.Names, ArchivedEntryHelpers.NameInfo))
			@ArchivedEntryHelpers.DataRow("Notes", comparedSongs, d => d.Notes, preserveLineBreaks: true)
			@ArchivedEntryHelpers.DataRow("Notes (en)", comparedSongs, d => d.NotesEng, preserveLineBreaks: true)
			@ArchivedEntryHelpers.DataRow("Song type", comparedSongs, d => d.SongType)
			@ArchivedEntryHelpers.DataRow("Original version", comparedSongs, d => ArchivedEntryHelpers.ObjectRefInfo(d.OriginalVersion))
			@ArchivedEntryHelpers.DataRow("Duration", comparedSongs, d => DateTimeUtils.FormatFromSeconds(d.LengthSeconds))
			@ArchivedEntryHelpers.DataRow("Release event", comparedSongs, d => ArchivedEntryHelpers.ObjectRefInfo(d.ReleaseEvent))
			@ArchivedEntryHelpers.DataRow("Publish date", comparedSongs, d => d.PublishDate)
			@ArchivedEntryHelpers.DataRowList("External links", comparedSongs, d => DataFormatUtils.GenerateHtmlFromHelper(d.WebLinks, ArchivedEntryHelpers.WebLinkInfo))
			@ArchivedEntryHelpers.DataRowList("Artists", comparedSongs, d => DataFormatUtils.GenerateHtml(d.Artists != null ? (d.Artists.OrderBy(a => a.NameHint)) : null, artist => MvcHtmlString.Create(artist.NameHint + " [" + artist.Id + "] - IsSupport: " + artist.IsSupport + ", Roles: " + artist.Roles)))
			@ArchivedEntryHelpers.DataRowList("PVs", comparedSongs, d => DataFormatUtils.GenerateHtmlFromHelper(d.PVs, ArchivedEntryHelpers.PVInfo))
			@ArchivedEntryHelpers.DataRowList("Lyrics", comparedSongs, d => DataFormatUtils.GenerateHtmlFromHelper(d.Lyrics, LyricsInfo))
		</table>

	</div>

}

@helper PVPreviewKnockout(string getPvServiceIcons) {
	<div data-bind="slideVisible: preview, complete: destroyPV" class="pvPreview" style="display: none;">
		<div data-bind="with: ratingButtons" class="pull-right">
			@{ Html.RenderPartial("Partials/_PVRatingButtonsForIndex"); }
			
			<!-- Binding context: SongWithPreviewViewModel -->
			@* PV service switcher *@
			<div class="btn-toolbar">
				<div class="btn-group">
					<div data-bind="visible: $parent.pvServices && $parent.pvServices.indexOf(',') > 0, foreach: @(getPvServiceIcons)($parent.pvServices)" class="pv-preview-services">
						<a class="btn" data-bind="click: function () { $parents[1].switchPV(service); }, attr: { title: service }, css: { active: service == $parents[1].pvService() }">
							<img data-bind="attr: { src: url }" />
						</a>
					</div>
				</div>
			</div>
		</div>
		<div data-bind="html: previewHtml"></div>
	</div>
}

@helper RatingIcon(SongVoteRating rating) {
	if (rating == SongVoteRating.Like) {
		<span class="icon starIcon" title="@Translate.SongVoteRatingNames[SongVoteRating.Like]"></span>
	}
	if (rating == SongVoteRating.Favorite) {
		<span class="icon heartIcon" title="@Translate.SongVoteRatingNames[SongVoteRating.Favorite]"></span>
	}	
}

@helper SongIconLink(SongContract song, int? albumId = null) {
	<a href="@Url.Action("Details", "Song", ViewHelper.GetRouteParams(song, albumId))">
		<img src="@VocaDb.Model.Service.Helpers.UrlHelper.UpgradeToHttps(song.ThumbUrl)" alt="Cover" class="coverPicThumb" />
	</a>		
}

@helper SongIconLink(SongForApiContract song) {
	<a href="@Url.SongDetails(song)">
		<img src="@VocaDb.Model.Service.Helpers.UrlHelper.UpgradeToHttps(song.ThumbUrl)" alt="Cover" class="coverPicThumb" />
	</a>
}

@helper SongLink(SongContract song, int? albumId = null, bool tooltip = false) {
	@Html.ActionLink(song.Name, "Details", "Song", ViewHelper.GetRouteValueDictionary(song, albumId), new Dictionary<string, object> { { "title", song.AdditionalNames }, { "data-bind", tooltip ? "songToolTip: " + song.Id : null } })
}

@helper SongLink(SongForApiContract song, int? albumId = null) {
	<a href="@Url.SongDetails(song, albumId)" title="@song.AdditionalNames">
		@song.Name
	</a>
}

@helper SongLinkKnockout(string binding, string extUrlBinding = null, bool tooltip = false, string toolTipDomainBinding = null) {
	<a href="#" data-bind="attr: { href: @extUrlBinding || vdb.utils.EntryUrlMapper.details_song(@binding), title: @(binding).additionalNames }, text: @(binding).name, css: { extLink: @extUrlBinding } @(tooltip ? ", songToolTip: " + binding + ".id" : "") @(toolTipDomainBinding != null ? ", toolTipDomain: " + toolTipDomainBinding : "")"></a>
}

@helper SongGrid(IEnumerable<SongForApiContract> songs, int columns,
	bool displayType = false, bool displayPublishDate = false) {

	<table>
		@{ int i = 0; }

		@foreach (var song in songs) {
			if (i % columns == 0) {
				@Html.Raw("<tr>")
			}
			<td>
				@if (!string.IsNullOrEmpty(song.ThumbUrl)) {
					@SongIconLink(song)
				}
			</td>
			<td>
				@SongLink(song)				
				@if (displayType) {
					@SongTypeLabel(song.SongType)
				}
				@if (displayPublishDate && song.PublishDate.HasValue) {
					<i class="icon-calendar" title="Published: @song.PublishDate.Value.ToShortDateString()" data-bind="tooltip: { placement: 'right' }"></i>
				}
				<br />
				<span class="extraInfo">@song.ArtistString</span>
			</td>
			{ i++; }
			if (i % columns == 0) {
				@Html.Raw("</tr>")
			}
		}
		@if (i % columns != 0) {
			@Html.Raw("</tr>")
		}
	</table>

}


@helper SongListsKnockout(string binding, bool groupByYear = false) {
	
	<table class="table table-striped">
		<tbody data-bind="foreach: @Html.Raw(binding)">
			<!-- ko if: @(groupByYear ? "true" : "false") && $parent.isFirstForYear($data, $index()) -->
			<tr>
				<td colspan="2">
					<h3 class="song-list-year">{{ $data.eventDate | formatDate: 'YYYY' }}</h3>
				</td>
			</tr>
			<!-- /ko -->
			<tr>
				<td style="width: 75px">
					<a href="#" data-bind="visible: $data.mainPicture && mainPicture.urlSmallThumb, attr: { href: vdb.utils.EntryUrlMapper.details('SongList', id) }">
						<img class="coverPicThumb" alt="Picture" data-bind="attr: { src: $data.mainPicture ? mainPicture.urlSmallThumb : '' }" />
					</a>
				</td>
				<td>
					<a href="#" data-bind="attr: { href: vdb.utils.EntryUrlMapper.details('SongList', id) }">{{ name }}</a>
					<div visible="{{ $data.eventDate }}">
						<small>{{ $data.eventDate | formatDate: 'l' }}</small>
					</div>
				</td>
			</tr>
		</tbody>
	</table>

}

@helper SongsList(IList<SongContract> songs, bool showType = false) {

	foreach (var song in songs) {
		if (showType) {
			@SongTypeLabel(song.SongType)
		}
		@SongLink(song)
		if (song != songs.Last()) {
			@(", ")
		}
	}
}

@helper SongTypeLabel(SongType songType) {
	switch (songType) {
		case SongType.Arrangement:
			<span class="label" title="@Translate.SongTypeNames[songType]">A</span>
			break;

		case SongType.Cover:
			<span class="label" title="@Translate.SongTypeNames[songType]">C</span>
			break;

		case SongType.DramaPV:
			<span class="label label-success" title="@Translate.SongTypeNames[songType]">D</span>
			break;		
			
		case SongType.Instrumental:
			<span class="label label-inverse" title="@Translate.SongTypeNames[songType]">I</span>
			break;
			
		case SongType.Mashup:
			<span class="label" title="@Translate.SongTypeNames[songType]">M</span>
			break;
			
		case SongType.Original:
			<span class="label label-info" title="@Translate.SongTypeNames[songType]">O</span>
			break;
			
		case SongType.Other:
			<span class="label" title="@Translate.SongTypeNames[songType]">O</span>
			break;
			
		case SongType.Remaster:
			<span class="label label-info" title="@Translate.SongTypeNames[songType]">R</span>
			break;
			
		case SongType.Remix:
			<span class="label" title="@Translate.SongTypeNames[songType]">R</span>
			break;
			
		case SongType.MusicPV:
			<span class="label label-success" title="@Translate.SongTypeNames[songType]">PV</span>
			break;		
	}
}

@helper SongTypesDropdownKnockout(string binding) {
	@KnockoutHelpers.CheckBoxButtons(Translate.SongTypeNames.GetValuesAndNamesStrings(AppConfig.SongTypes), binding);
}